package com.indigon.parkStory.view.objects
{
	import com.indigon.parkStory.model.objects.ObjectModel;
	import com.indigon.parkStory.view.ViewMain;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;

	public class ObjectView
	{
		public var graphic    : MovieClip;
		public var removed    : Boolean;
		protected var _object : ObjectModel;
		
		public function ObjectView(graphic:MovieClip, object:ObjectModel, stop:Boolean=true):void
		{
			this._object = object;
			this.graphic = graphic;
			if (stop)
			{
				this.graphic.stop();
			}
		}
		
		public function set(container:DisplayObjectContainer, x:Number, y:Number, rotation:Number=0, layer:uint=0):void
		{
			graphic.x = x;
			graphic.y = y;
			graphic.rotation = rotation;
			container.addChildAt(graphic, layer);
		}
		
		public function uninit():void
		{
			if (!removed)
			{
				graphic.parent.removeChild(graphic);
				graphic = null;
				_object = null;
				removed = true;
			}
		}
		
		public function update():void
		{
			graphic.rotation += _object.angVel;
			place(_object.pos.x, _object.pos.y);
		}
		
		protected function place(x:Number, y:Number):void
		{
			graphic.x = ViewMain.CENTER_OFFSET.x + x;
			graphic.y = ViewMain.CENTER_OFFSET.y + y;
		}
	}
}